import List from "../components/List";
import Marquee from "../components/Marquee";
import FrameLoading from "../core/FrameLoading";
import ResManager from "../core/ResManager";
import UIBase from "../core/UIBase";
import HotUpdateModule from "../module/HotUpdateModule";
import LogUtil from "../utils/LogUtil";
import NodeUtil from "../utils/NodeUtil";
import ObjectUtil from "../utils/ObjectUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class HallView extends UIBase {

    @property(List)
    list: List = null;

    @property(cc.Node)
    indicator: cc.Node = null;
    
    @property(cc.Node)
    sign: cc.Node = null;

    @property({ type: cc.Prefab, tooltip: CC_DEV && '小游戏预制' })
    public GameItem: cc.Prefab = null;

    @property(Marquee)
    marquee: Marquee = null

    games: cc.Node = null;
    lbVersion: cc.Label = null;
    
    private _listData = [];
    private _totalItemNum: number = 32;  //总Item数
    private _pagePreNum: number = 6;    //每页Item数量
    private _pageTotalNum: number;       //总页数
    private _signList = [];

    protected onLoad() {
        super.onLoad();
        this.games = this.node.getChildByName('games');
        let lab = NodeUtil.getChildByName(this.node,'version',cc.Label);
        lab && (lab.string = HotUpdateModule.ins().getVersion());
    }

    init(args:any) {
        this.marquee.push(['大家好，我是小明', '欢迎欢迎欢迎欢迎欢迎！']);
        this.marquee.play();
    }

    start() {
        let config = ResManager.ins().getRes('config_smallGame');
        this._listData = ObjectUtil.objConvertArray(config);
        LogUtil.log(LogUtil.getFileName(),this._listData);
        
        this._totalItemNum = this._listData.length;
        this._pageTotalNum = Math.ceil(this._totalItemNum / this._pagePreNum);//总页数
        this.list.numItems = this._pageTotalNum;
        this.onPageChange();
        
        this._signList.push(this.sign);
        for(let i = 1; i < this._pageTotalNum; i++){
            let node = cc.instantiate(this.sign);
            this.indicator.addChild(node);
            this._signList.push(node);
        }
        this.updateSignColor(0);
    }

    updateSignColor(pageN: number) {
        if(this._signList.length == 1){
            this._signList[0].active = false;
            return;
        }
        for(let i = 0; i < this._signList.length; i++){
            let node: cc.Node =  this._signList[i];
            if(pageN == i){
                node.color = new cc.Color(255,240,40);
            }else{
                node.color = new cc.Color(255,255,255);
            }
        }
    }

    onListRender(item: cc.Node, idx: number) {
        if (item.childrenCount) {
            for (let n = 0; n < item.childrenCount; n++) {
                let node: any = item.children[n];
                let exactIdx = (idx * this._pagePreNum) + n;
                let ctrl = node.getComponent('GameItem');
                ctrl.init(this._listData[exactIdx]);
            }
        } else {
            //分帧加载
            FrameLoading.ins().loadByFrames(this.GameItem, this._pagePreNum, 1, item, this, (node, n) => {
                let ctrl = node.getComponent('GameItem');
                let exactIdx = (idx * this._pagePreNum) + n;
                ctrl && ctrl.init(this._listData[exactIdx]);
            });
        }
    }

    onPageChange(pageNum: number = null) {
        let pageN = pageNum == null ? this.list.curPageNum : pageNum;
        LogUtil.log(LogUtil.getFileName(),'当前页数：' + (pageN + 1));
        this.updateSignColor(pageN);
    }
}
